<?php

namespace Model;

class PostList
{
		//Privata strängar
		private $db = NULL;
        private $dbt = NULL;
		
		//Konstrukterar databas & DBtabell instans
        public function __construct(Database $db) 
        {
                $this->db =$db;
				$this->dbt = new \Model\DBTables();	
        }		
		
		//Returnerar en array med Post-Objekt tillhörande medskickat topicId
		public function GetPostsByTopicId($topicId)
		{
			$sql = "SELECT p.postId, p.topicId, p.userId, t.header, u.name, p.date, p.content
					FROM ". $this->dbt->postTable ." AS p
					INNER JOIN ". $this->dbt->topicTable ." AS t
					ON p.topicId = t.topicId
					INNER JOIN ". $this->dbt->userTable ." AS u
					ON p.userId = u.userId
					WHERE p.topicId =?
					ORDER BY p.date ASC";
									
			$stmt = $this->db->Prepare($sql);
			
			$stmt->bind_param("i", $topicId);
			
			$stmt = $this->db->Select($stmt);
			
			if($stmt->bind_result($postId, $topicId, $userId, $topicHeader, $userName, $date, $content) == FALSE)
			{
				throw new \Exception($this->mysqli->error);
			}	
						
			$ret = new PostArray();
			
			while ($stmt->fetch())
			{
				$post = new Post($postId, $topicId, $userId, $topicHeader, $userName, $date, $content);
				$ret->add($post);
			}
			
			$stmt->close();
			
			return $ret;			
		}

		//Returnerar en array med Post-Objekt tillhörande medskickat userId		
		public function GetPostsByUserId($userId)
		{
			$sql = "SELECT p.postId, p.topicId, p.userId, t.header, u.name, p.date, p.content
					FROM ". $this->dbt->postTable ." AS p
					INNER JOIN ". $this->dbt->topicTable ." AS t
					ON p.topicId = t.topicId
					INNER JOIN ". $this->dbt->userTable ." AS u
					ON p.userId = u.userId
					WHERE p.userId =?
					ORDER BY p.date DESC";
				
			$stmt = $this->db->Prepare($sql);
			
			$stmt->bind_param("i", $userId);
			
			$stmt = $this->db->Select($stmt);
			
			if($stmt->bind_result($postId, $topicId, $userId, $topicHeader, $userName, $date, $content) == FALSE)
			{
				throw new \Exception($this->mysqli->error);
			}	
						
			$ret = new PostArray();
			
			while ($stmt->fetch())
			{
				$post = new Post($postId, $topicId, $userId, $topicHeader, $userName, $date, $content);
				$ret->add($post);
			}
			
			$stmt->close();
			
			return $ret;				
		}

		//Returnerar postId tillhörande det senast skapade post-objektet
		public function GetLastAddedPostId()
		{
			$sql = "SELECT MAX(postId)
					FROM ". $this->dbt->postTable;
					
			$stmt = $this->db->Prepare($sql);		
			
			$ret = $this->db->SelectSingleValue($stmt);
			
			return $ret;				
		}

		//Returnerar TRUE vid lyckat skapande av ny Post	
		public function AddPost($topicId, $userId, $content) 
		{						
			$sql = "INSERT INTO ". $this->dbt->postTable ." (topicId, userId, content) VALUES (?,?,?)";  
			
			$stmt = $this->db->Prepare($sql); 

			$stmt->bind_param("iis", $topicId, $userId, $content);			      
			
			$exec = $this->db->Execute($stmt);
			
			return TRUE;
		}
		
		//Returnerar TRUE vid lyckat borttagande av Post
		public function DeletePost($postId)
		{
			$sql = "DELETE p.*
					FROM ". $this->dbt->postTable ." AS p
					WHERE p.postId=?";
			
			$stmt = $this->db->Prepare($sql); 

			$stmt->bind_param("i", $postId);			      
			
			$exec = $this->db->Execute($stmt);
			
			return TRUE;			
		}
		
		//Anropar "FormValidator" för validering av postinnehåll
		//Returnerar array innehållande felmeddelanden
		public function ValidateNewPostInput($content)
		{
			$fv = new \FormValidator();
			
			$ret = $fv->ValidateContent($content);

			return $ret;			
		}
		
		//AUTOMATISK TESTFUNKTION								
		public static function Test(\Model\PostList $pl, \Model\TopicList $tl)
		{
			//Skapar ett topic för testsyfte			
			$tl->AddTopic("1", "testTopic", "content");
			//Hämtar id från det nyss skapade topic
			$topicId = $tl->GetLastAddedTopicId();
			
			//Testar AddPost
			if($pl->AddPost($topicId, "1", "content2") != TRUE)
			{
				echo "AddPost did not return TRUE";
				return FALSE;				
			}
			
			//Testar GetPostsByTopicId
			$ret = get_class($pl->GetPostsByTopicId($topicId));
			if($ret != "Model\PostArray")
			{
				echo "GetPostsByTopicId returned " .$ret ;
				return FALSE;					
			}
			
			//Testar GetPostsByUserId
			$ret = get_class($pl->GetPostsByUserId("1"));			
			if($ret != "Model\PostArray")
			{
				echo "GetPostsByUserId returned " .$ret ;
				return FALSE;					
			}		

			//Testar DeletePost
			$postId = $pl->GetLastAddedPostId();
			
			if($pl->DeletePost($postId) != TRUE)
			{
				echo "DeletePostt did not return TRUE";
				return FALSE;				
			}

			//Tar bort test objektet
			$tl->DeleteTopic($topicId);
			
			return TRUE;			
		}
}

?>